<template>
	<view class="content" @click="allClick">
		<view class="bg"></view>
		<u-navbar fixed title="我的乐谱" bgColor="transparent" placeholder autoBack></u-navbar>

		<view class="main">
			<view class="box">
				<view class="box_1" v-for="(item, index) in tabData" :key="index" :class="{ active: tabindex == index }" @click="changeTab(index)">{{ item }}</view>
			</view>

			<view class="box1">
				<scroll-view class="scroll-view_H" :scroll-x="true">
					<!-- <view class="box1_1" v-for="(item, index) in tabData1" :key="index"
						:class="{ active: item.id == tabindex }" :id="'tab' + item.id" @click="changeTab(index, item)">
						<text>{{ item.name }}</text>
					</view> -->

					<view class="" style="display: inline-block" @longpress="changeTopType1()">
						<u-tag
							class="box1_1"
							v-for="(item, index) in tabData1"
							:key="index"
							:text="item.name"
							:bgColor="topindex == item.id ? 'rgb(23, 173, 110)' : 'rgb(235, 255, 246)'"
							:color="topindex == item.id ? '#fff' : 'rgb(23, 173, 110)'"
							borderColor="transparent"
							size="large"
							:closable="item.user == 0 ? false : closable"
							@click="changeTopType(item, index)"
							@close="changeDelTop(item, index)"
						></u-tag>
					</view>
				</scroll-view>
			</view>

			<view class="box2" id="mainbox">
				<view class="box2_l">
					<scroll-view scroll-y="true" class="scroll_Y_l">
						<view
							class="scroll-view-item"
							v-for="(item, index) in leftData"
							:key="index"
							:class="{ active: item.id == leftindex }"
							@click="changeLeft(index, item)"
							@longpress="changeType(index, item)"
						>
							{{ item.name }}
						</view>
					</scroll-view>
					<view
						class="tooltips"
						v-if="tooltipsShow"
						:style="{
							'--top': tooltipsTop,
							'--left': lleft,
							'--aftertop': tooltipsAfterTop,
							'--afterleft': lafterleft,
							'--lwidth': lwidth,
							'--lheight': lheight,
							'--flexdirection': lflex,
							'--bordercolor': lborder
						}"
					>
						<view class="tooltips_item" @click="handleEdit(1)">
							<image src="../../static/icon/icon9.png" mode="heightFix"></image>
							<text>重命名</text>
						</view>
						<view class="tooltips_item" @click="handleDel(1)">
							<image src="../../static/icon/icon10.png" mode="heightFix"></image>
							<text>删除</text>
						</view>
					</view>
				</view>

				<view class="box2_r">
					<scroll-view scroll-y="true" class="scroll_Y_r">
						<view class="box4_1" v-for="(item, index) in listData" :key="index" @click="changeMusicItem(item, index)" @longpress="changeSongs(index, item)">
							<view class="box4_1_l">
								<view>{{ item.name }}</view>
								<view>
									<text>作者：{{ item.author || '--' }}</text>
									<text></text>
								</view>
							</view>
						</view>
					</scroll-view>
					<view
						class="tooltips"
						v-if="tooltipsShow1"
						:style="{
							'--top': tooltipsTop1,
							'--left': rleft,
							'--aftertop': rftertop,
							'--afterleft': rafterleft,
							'--lwidth': rwidth,
							'--lheight': rheight,
							'--flexdirection': rflex,
							'--bordercolor': rborder
						}"
					>
						<view class="tooltips_item" @click="handleEdit(2)">
							<image src="../../static/icon/icon9.png" mode="heightFix"></image>
							<text>重命名</text>
						</view>
						<view class="tooltips_item" @click="handleDel(2)">
							<image src="../../static/icon/icon10.png" mode="heightFix"></image>
							<text>删除</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import API from '@/utils/api.js';
import { handleTree } from '@/utils/index.js';
export default {
	data() {
		return {
			tabindex: 0,
			father_id: 0,
			tabData: ['图片乐谱', 'PDF乐谱'],

			topindex: 0,
			tabData1: [],
			listData: [],
			leftData: [],

			leftindex: 0,
			manboxHeight: 0,
			longpressData: null,
			longpressIndex: null,

			tooltipsShow: false,
			tooltipsTop: '0px',
			tooltipsAfterTop: '20px',

			tooltipsShow1: false,
			tooltipsTop1: '90px',

			closable: false,
			needDelSongsData: []
		};
	},
	computed: {
		lleft() {
			return uni.upx2px(210) + 'px';
		},
		lwidth() {
			return uni.upx2px(100) + 'px';
		},
		lheight() {
			return uni.upx2px(300) + 'px';
		},
		lflex() {
			return 'column';
		},
		lborder() {
			return 'transparent rgba(0, 0, 0, 0.5) transparent transparent';
		},
		lafterleft() {
			return '0px';
		},
		rleft() {
			return '20px';
		},
		rwidth() {
			return uni.upx2px(300) + 'px';
		},
		rheight() {
			return uni.upx2px(100) + 'px';
		},
		rflex() {
			return 'row';
		},
		rborder() {
			return 'transparent transparent rgba(0, 0, 0, 0.5) transparent';
		},
		rftertop() {
			return '0px';
		},
		rafterleft() {
			return uni.upx2px(300 - 20) / 2 + 'px';
		}
	},
	methods: {
		allClick() {
			this.tooltipsShow = false;
			this.tooltipsShow1 = false;
		},

		// 返回
		clickLeft() {
			uni.navigateBack();
		},

		// 切换
		changeTab(index) {
			this.tabindex = index;
			this.tooltipsShow = false;
			this.getList('', '');
		},

		// 一级菜单切换
		changeTopType(item, index) {
			this.topindex = item.id;
			this.leftindex = this.tabData1[index].children ? this.tabData1[index].children[0].id : 0;
			this.leftData = item.children;
			this.tooltipsShow = false;
			this.tooltipsShow1 = false;
			this.closable = false;
			this.getSongs();
		},

		// 长按一级标题
		changeTopType1(item, index) {
			console.log(item, '长按一级标题');
			this.tooltipsShow = false;
			this.tooltipsShow1 = false;
			this.closable = true;
		},

		// 顶级菜单删除
		async changeDelTop(item, index) {
			console.log('删除的顶级菜单', item);
			let promeis = [];
			promeis.push(API.deleteRecord(item.id, 0));
			this.needDelSongsData = [];
			if (item.children.length) {
				for (let i = 0; i < item.children.length; i++) {
					promeis.push(API.deleteRecord(item.children[i].id, 0));
					let data = await this.getdelSongs(item.children[i].id);
					this.needDelSongsData = this.needDelSongsData.concat(data);
				}
			}

			if (this.needDelSongsData.length) {
				console.log('需要删除的歌曲', this.needDelSongsData);
				for (let i = 0; i < this.needDelSongsData.length; i++) {
					promeis.push(API.deleteRecord(this.needDelSongsData[i].id, 1));
					let arr = this.needDelSongsData[i].urls.split(',');
					for (let k = 0; i < arr.lengtn; k++) {
						promeis.push(API.deleteRecord(arr[k], 2));
					}
				}
			}

			Promise.all(promeis).then(
				(res) => {
					console.log('删除二级分类', res);
					uni.showToast({
						icon: 'success',
						title: '删除成功'
					});
					this.needDelSongsData = [];
					this.getList('', '');
				},
				(err) => {
					console.log('删除失败', err);
				}
			);
		},

		async getdelSongs(id) {
			// 根据type获取列表数据,并且user = 1
			let sql = `WHERE father_id=${id} AND user=1`;
			let data = await API.queryTableData(sql);
			return data;
		},

		// 切换侧边菜单
		changeLeft(index, item) {
			this.leftindex = item.id;
			this.tooltipsShow = false;
			this.getSongs();
		},

		// 二级分类长按
		changeType(index, item) {
			console.log(item, '二级分类');
			this.needDelSongsData = [];
			if (item.user == 0) return;
			this.tooltipsShow = true;
			this.tooltipsShow1 = false;
			this.closable = false;
			this.longpressData = item;
			this.longpressIndex = index;
			let topnum = index * uni.upx2px(105);
			if (topnum > this.manboxHeight) {
				this.tooltipsTop = this.manboxHeight - uni.upx2px(300) + 'px';
				this.tooltipsAfterTop = uni.upx2px(300) - (uni.upx2px(300) - 20) + 'px';
			} else {
				this.tooltipsTop = topnum + 'px';
				this.tooltipsAfterTop = '20px';
			}
			console.log('长按的参数', index, this.tooltipsTop);
		},

		// 乐谱长按
		changeSongs(index, item) {
			if (item.user == 0) return;
			this.tooltipsShow1 = true;
			this.tooltipsShow = false;
			this.closable = false;
			this.longpressData = item;
			this.longpressIndex = index;
			let topnum = (index + 1) * uni.upx2px(170);
			if (topnum > this.manboxHeight) {
				this.tooltipsTop1 = this.manboxHeight - uni.upx2px(100) + 'px';
			} else {
				this.tooltipsTop1 = topnum + 'px';
			}
			console.log('长按的参数', index, this.tooltipsTop);
		},

		// 查看乐谱详情
		changeMusicItem(item, index) {
			console.log(item, index, '查看歌曲');
			uni.navigateTo({
				url: `/pages/ownmusic/detail?id=${item.id}`
			});
		},

		// 删除侧栏、乐谱
		handleDel(val) {
			console.log('删除的类型', val);
			uni.showModal({
				title: '提示',
				content: '确定删除吗？',
				success: (res) => {
					if (res.confirm) {
						if (val == 1) {
							this.delTyps(this.longpressData, this.longpressIndex);
						} else {
							this.delSongs(this.longpressData, this.longpressIndex);
						}
					} else if (res.cancel) {
					}
				}
			});
		},

		// 删除二级分类
		async delTyps(item, index) {
			console.log('删除的二级分类数据', item);
			let promeis = [];
			promeis.push(API.deleteRecord(item.id, 0));

			let data = await this.getdelSongs(item.id);
			this.needDelSongsData = this.needDelSongsData.concat(data);

			if (this.needDelSongsData.length) {
				for (let i = 0; i < this.needDelSongsData.length; i++) {
					promeis.push(API.deleteRecord(this.needDelSongsData[i].id, 1));
					let arr = this.needDelSongsData[i].urls.split(',');
					for (let k = 0; i < arr.lengtn; k++) {
						promeis.push(API.deleteRecord(arr[k], 2));
					}
				}
			}

			Promise.all(promeis).then(
				(res) => {
					console.log('删除二级分类', res);
					uni.showToast({
						icon: 'success',
						title: '删除成功'
					});
					this.needDelSongsData = [];
					this.getList(this.topindex, 0);
				},
				(err) => {
					console.log('删除失败', err);
				}
			);
		},

		// 删除乐谱
		delSongs(item, index) {
			console.log('删除的乐谱数据', item);
			let arr = item.urls.split(',');
			let promeis = [];
			for (let i = 0; i < arr.lengtn; i++) {
				promeis.push(API.deleteRecord(arr[i], 2));
			}
			promeis.push(API.deleteRecord(item.id, 1));
			// API.getFileList(item.id);
			Promise.all(promeis).then(
				(res) => {
					console.log('删除乐谱', res);
					uni.showToast({
						icon: 'success',
						title: '删除成功'
					});
					this.getList(this.topindex, this.leftindex);
				},
				(err) => {
					console.log('删除失败', err);
				}
			);
		},

		// 	重命名
		handleEdit(val) {
			console.log('重命名的类型', val);
			if (val == 1) {
				this.editTyps(this.longpressData, this.longpressIndex);
			} else {
				this.editSongs(this.longpressData, this.longpressIndex);
			}
		},

		// 重命名二级分类
		editTyps(item, index) {
			console.log('重命名的二级分类数据', item);
			uni.showModal({
				title: '提示',
				placeholderText: '请输入新的名称',
				editable: true,
				success: (res) => {
					if (res.confirm) {
						console.log('输入的新名称', res);
						let name = res.content;
						if (name) {
							console.log('新数据', name);
							API.recordSalary({
								id: item.id,
								name: name,
								father_id: item.father_id,
								user: item.user
							}).then((res) => {
								console.log('修改二级分类成功', res);
								uni.showToast({
									icon: 'success',
									title: '修改成功'
								});
								this.getList(this.topindex, this.leftindex);
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: '请输入新的名称'
							});
						}
					} else if (res.cancel) {
					}
				}
			});
		},

		// 重命名乐谱
		editSongs(item, index) {
			console.log('重命名的乐谱数据', item);
			uni.showModal({
				title: '提示',
				placeholderText: '请输入新的名称',
				editable: true,
				success: (res) => {
					if (res.confirm) {
						console.log('输入的新名称', res);
						let name = res.content;
						if (name) {
							console.log('新数据', name);
							API.recordSong({
								id: item.id,
								name: name,
								usls: item.urls,
								type: item.type,
								author: item.author,
								album: item.album,
								father_id: item.father_id,
								user: item.user
							}).then((res) => {
								console.log('修改二级分类成功', res);
								uni.showToast({
									icon: 'success',
									title: '修改成功'
								});
								this.getList(this.topindex, this.leftindex);
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: '请输入新的名称'
							});
						}
					} else if (res.cancel) {
					}
				}
			});
		},

		getList(val, val1) {
			API.getAllCategories().then((res) => {
				console.log('查询的所有分类', res);
				this.tabData1 = handleTree(res);
				console.log(this.tabData1);
				if (val) {
					this.topindex = val;
					this.leftData = this.tabData1.filter((item) => item.id == val)[0].children;
				} else {
					this.topindex = this.tabData1[0].id;
					this.leftData = this.tabData1[0].children;
				}

				// this.leftData = res.filter((item) => item.father_id != 0);
				// this.leftindex = this.leftData[0].id;

				if (val1) {
					this.leftindex = val1;
				} else {
					this.leftindex = this.tabData1[0].children ? this.tabData1[0].children[0].id : 0;
				}

				this.getSongs();
			});
		},

		getSongs() {
			// 根据type获取列表数据,并且user = 1
			let sql = `WHERE type=${this.tabindex} AND father_id=${this.leftindex} AND user=1`;
			API.queryTableData(sql)
				.then((res) => {
					console.log(res, '查询的乐谱列表');
					this.listData = res;
				})
				.catch((err) => {
					console.log(err);
				});
		},

		getMainBoxHeight() {
			let query = uni.createSelectorQuery().in(this);
			query.select('#mainbox').boundingClientRect();
			query.exec((res) => {
				console.log('获取节点的信息', res);
				this.manboxHeight = res[0].height;
			});
		}
	},
	onShow() {
		this.getList();
		this.$nextTick(() => {
			this.getMainBoxHeight();
		});
	}
};
</script>

<style lang="scss" scoped>
.content {
	position: relative;
	overflow: hidden;

	.bg {
		width: calc(100vw + 270rpx);
		height: 282rpx;
		background-color: rgba(23, 183, 109, 0.61);
		filter: blur(180rpx);
		position: absolute;
		top: 0rpx;
		left: -210rpx;
		z-index: 1;
	}

	.main {
		width: 100%;
		height: calc(100vh - 44px - var(--status-bar-height));
		overflow: hidden;
		position: relative;
		z-index: 2;
	}

	.box {
		width: 100%;
		padding: 0 22rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 50rpx 0 30rpx 0;

		view {
			flex: 1 0;
			width: 0;
			height: 85rpx;
			background: #ecfff6;
			border-radius: 12rpx 12rpx 12rpx 12rpx;
			margin: 0 8rpx;
			text-align: center;
			font-family: Source Han Sans, Source Han Sans;
			font-weight: 500;
			font-size: 36rpx;
			color: #17b76d;
			line-height: 84rpx;
		}

		.active {
			background: #17b76d;
			color: #ffffff;
		}
	}

	.box1 {
		width: 100%;
		// height: 65rpx;
		padding: 0 22rpx;
		margin-bottom: 25rpx;

		.scroll-view_H {
			width: 100%;
			white-space: nowrap;
			// overflow-y: auto;

			.box1_1 {
				display: inline-block;
				// min-width: 128rpx;
				// height: 65rpx;
				// padding: 0 10rpx;
				// color: rgb(23, 173, 110);
				// background-color: rgb(235, 255, 246);
				margin: 0 12rpx;
				// border-radius: 12rpx 12rpx 12rpx 12rpx;
				// font-family: Source Han Sans, Source Han Sans;
				// font-weight: 500;
				// font-size: 28rpx;
				// color: #17b76d;
				// line-height: 65rpx;
				// text-align: center;
			}

			// .box1_1:nth-child(1) {
			// 	margin-left: 0;
			// }

			// .box1_1:nth-last-child(1) {
			// 	margin-right: 0;
			// }

			// .active {
			// 	color: #fff;
			// 	background-color: rgb(23, 173, 110);
			// }
		}
	}

	.box2 {
		width: 100%;
		height: calc(100vh - 44px - 165rpx - var(--status-bar-height));
		display: flex;
		justify-content: space-between;
		overflow: hidden;
		background-color: #fff;
		position: relative;
		z-index: 2;

		.box2_l,
		.box2_r {
			position: relative;
		}

		.scroll_Y_l,
		.scroll_Y_r {
			height: calc(100vh - 44px - 165rpx - var(--status-bar-height));
		}

		.scroll_Y_l {
			width: 190rpx;
			border-right: 1rpx solid #dbdbdb;
			border-top: 1rpx solid #dbdbdb;

			.scroll-view-item {
				width: 190rpx;
				height: 105rpx;
				font-family: Source Han Sans, Source Han Sans;
				font-weight: 500;
				font-size: 32rpx;
				color: #333333;
				line-height: 105rpx;
				text-align: center;
				border-bottom: 1rpx solid #dbdbdb;
				// 超出部分隐藏
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.active {
				position: relative;

				&::before {
					content: '';
					position: absolute;
					top: 0;
					left: 0;
					width: 8rpx;
					height: 105rpx;
					background-color: #17b76d;
					box-shadow: 15rpx 0rpx 50rpx #17b76d;
				}
			}
		}

		.scroll_Y_r {
			width: calc(100vw - 205rpx);
			padding: 20rpx 0;

			.box4_1 {
				width: calc(100% - 20rpx);
				height: 144rpx;
				box-shadow: 0rpx 6rpx 16rpx 0rpx rgba(0, 0, 0, 0.08);
				display: flex;
				align-items: center;
				justify-content: space-between;
				background-color: #ffffff;
				padding: 0 18rpx 0 26rpx;
				margin-bottom: 8rpx;

				.box4_1_l {
					flex: 1 0;
					width: 0;

					view:nth-child(1) {
						font-weight: 500;
						font-size: 36rpx;
						color: #333333;
						line-height: 52rpx;
						text-align: left;
						margin-bottom: 16rpx;
					}

					view:nth-child(2) {
						font-weight: 400;
						font-size: 28rpx;
						color: #666666;
						line-height: 40rpx;
						text-align: left;
						display: flex;
						align-items: center;

						text {
							flex: 1;
						}
					}
				}
			}
		}

		.tooltips {
			position: absolute;
			top: var(--top);
			left: var(--left);
			z-index: 9;
			width: var(--lwidth);
			height: var(--lheight);
			padding: 20rpx 0;
			background-color: rgba(0, 0, 0, 0.5);
			border-radius: 10rpx;
			display: flex;
			flex-direction: var(--flexdirection);
			align-items: center;
			justify-content: space-around;

			.tooltips_item {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				margin: 10rpx 0;

				image {
					height: 50rpx;
				}

				text {
					font-size: 28rpx;
					color: #ffffff;
				}
			}
		}

		.tooltips::after {
			content: '';
			position: absolute;
			top: var(--aftertop);
			left: var(--afterleft);
			transform: translate(-100%, -100%);
			width: 0;
			height: 0;
			border-width: 10rpx;
			border-style: solid;
			border-color: var(--bordercolor);
		}
	}
}
</style>